package com.runfast.waimai.web.controller.gallery;

import com.runfast.common.utils.SessionContext;
import com.runfast.common.web.entity.Result;
import com.runfast.common.web.entity.ResultCode;
import com.runfast.waimai.dao.model.*;
import com.runfast.waimai.service.RunfastGalleryCategoryService;
import com.runfast.waimai.service.RunfastGalleryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.awt.*;

/**
 * @author: Mr.Jiao
 * @date: 2018年08月15日
 */
@RestController(value = "galleryCategoryController")
@RequestMapping(value = "/server/gallerycategory/")
public class GalleryCategoryController {

    @Autowired
    private RunfastGalleryCategoryService galleryCategoryService;

    @Autowired
    private RunfastGalleryService galleryService;

    /**
     * 图库分类
     * @return
     */
    @GetMapping(value = "/list", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Result<?> list(){
        RunfastGalleryCategoryExample example = new RunfastGalleryCategoryExample();
        return Result.ok("ok", galleryCategoryService.selectByExample(example));
    }


    /**
     * 保存
     * @param category
     * @return
     */
    @PostMapping(value = "/save", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Result<?> save(@Validated(value = RunfastGalleryCategory.Insert.class) RunfastGalleryCategory category){
        BaseUser admin = SessionContext.getAdmin();
        if(admin != null && admin.getType() == 1) {
            galleryCategoryService.insertSelective(category);
            return Result.ok("ok");
        }else{
            return Result.fail(ResultCode.FAIL,"权限不足");
        }
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @PostMapping(value = "/del", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Result<?> del(Integer id){
        if(id == null){
            return Result.fail(ResultCode.FAIL,"图库类别ID错误!");
        }
        BaseUser admin = SessionContext.getAdmin();
        if(admin != null && admin.getType() == 1) {
            RunfastGalleryCategory category = new RunfastGalleryCategory();
            category.setDel(true);
            RunfastGalleryCategoryExample example = new RunfastGalleryCategoryExample();
            example.createCriteria().andIdEqualTo(id).andDelEqualTo(false);
            if(galleryCategoryService.updateByExampleSelective(category, example) > 0) {
                //设置图片状态为删除
                RunfastGalleryExample galleryexample = new RunfastGalleryExample();
                galleryexample.createCriteria().andEnableEqualTo(true).andCategoryidEqualTo(id);
                RunfastGallery gallery = new RunfastGallery();
                gallery.setEnable(false);
                galleryService.updateByExampleSelective(gallery, galleryexample);
                return Result.ok("ok");
            }else{
                return Result.fail(ResultCode.FAIL, "删除失败");
            }
        }else{
            return Result.fail(ResultCode.FAIL,"权限不足");
        }
    }

    /**
     * 修改图库类别
     * @param category
     * @return
     */
    @PostMapping(value = "/modify", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Result<?> modify(@Validated(value = {RunfastGalleryCategory.Modify.class}) RunfastGalleryCategory category){
        BaseUser admin = SessionContext.getAdmin();
        if(admin != null && admin.getType() == 1) {
            galleryCategoryService.updateByPrimaryKeySelective(category);
            return Result.ok("ok");
        }else{
            return Result.fail(ResultCode.FAIL,"权限不足");
        }
    }
}
